package com.example.smartclassroom.controller;
import com.example.smartclassroom.entity.Admin;
import com.example.smartclassroom.entity.form.LoginForm;
import com.example.smartclassroom.entity.form.RegisterForm;
import com.example.smartclassroom.enums.BorrowStatusEnum;
import com.example.smartclassroom.model.MyResponseData;
import com.example.smartclassroom.service.impl.AdminServiceImpl;
import com.example.smartclassroom.service.LogInService;
import com.example.smartclassroom.util.JsonDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminServiceImpl adminServiceImpl;

    @Autowired
    private LogInService logInService;

    /**
     * 登录
     * @param loginForm
     * @return
     */
    @PostMapping("/user-login")
    public JsonDate userLogin(@RequestBody LoginForm loginForm) {
        String token = logInService.login(loginForm);
        return  JsonDate.successEnum(BorrowStatusEnum.LOGIN_SUCCESS,token);
    }

    /**
     * 注册md5加密
     * @param registerForm
     * @return
     */
    @PostMapping("/register")
    public JsonDate register(@RequestBody RegisterForm registerForm) {
        JsonDate register = logInService.register(registerForm);
         return  register;
    }


    /**
     * 查询用户信息
     *
     * // 1 保证登陆成功
     * //2  根据用户id 和用户类型 查询自己的信息 返回用户id 名称  用户头像 不返回密码
     */
    @GetMapping("/user-info")
    public JsonDate userInfo() {
        return JsonDate.success(logInService.userInfo());
    }


    /**
     * 编辑用户信息
     * @param admin
     * @return
     */
    @RequestMapping("edit")
    public MyResponseData edit(Admin admin) {
        MyResponseData data = new MyResponseData();
        try {
            Integer count = adminServiceImpl.update(admin);
            if (count > 0) {
                data.setMsg("修改成功");
                return data;
            } else {
                data.setMsg("修改失败");
                return data;
            }
        }catch (RuntimeException e){
            e.printStackTrace();
        }
        return data;
    }
}
